package com.sixth.service.impl;


import com.github.pagehelper.PageHelper;
import com.sixth.bean.MarketCategory;
import com.sixth.bean.MarketCategoryExample;
import com.sixth.bean.MarketGoods;
import com.sixth.bean.MarketGoodsExample;
import com.sixth.bean.bo.CategoryCreateBO;
import com.sixth.bean.bo.CategoryUpdateBO;
import com.sixth.bean.vo.*;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketCategoryMapper;
import com.sixth.mapper.MarketGoodsMapper;
import com.sixth.service.CategoryService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 商品类目模块的service
 *
 * @Author lixinhang
 * @Time 2022/9/7 9:21
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CategoryL1VO categoryL1() {
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo(Constant.FIRST_LEVEL);
        //查询所有level为L1的类目的数量
        long count = marketCategoryMapper.countByExample(example);
        Integer total = (int)count;
        Integer limit = total;
        Integer pages = 1; //pages总为1
        Integer page = 1;  //page总为1

        //查询所有level为L1的类目，得到一个list
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        List<LabelAndValueOfCategoryL1VO> labelAndValueOfCategoryL1VOS = new LinkedList<LabelAndValueOfCategoryL1VO>();

        for (MarketCategory marketCategory : marketCategories) {
            LabelAndValueOfCategoryL1VO labelAndValueOfCategoryL1VO = new LabelAndValueOfCategoryL1VO(marketCategory.getName(), marketCategory.getId());
            labelAndValueOfCategoryL1VOS.add(labelAndValueOfCategoryL1VO);
        }

        CategoryL1VO categoryL1VO = new CategoryL1VO();
        categoryL1VO.setTotal(total);
        categoryL1VO.setPage(page);
        categoryL1VO.setPages(pages);
        categoryL1VO.setLimit(limit);
        categoryL1VO.setList(labelAndValueOfCategoryL1VOS);

        return categoryL1VO;
    }

    //查询所有类目的方法
    @Override
    public CategoryListVO categoryList() {
        //查询所有pid为0的类目
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);

        long count = marketCategoryMapper.countByExample(marketCategoryExample);
        Integer total = (int) count;
        Integer limit = total;
        Integer pages = 1;  //pages总为1
        Integer page = 1;   //page总为1

        //查询全部的pid为0的类目，连接查询，对于pid非零的类目，去找id可以匹配自己pid的类目
        List<InnerCategoriesOfCategoryListVO> list = marketCategoryMapper.selectFirstAndSeondCategoriesUseCrossQuery();
        CategoryListVO categoryListVO = new CategoryListVO();
        categoryListVO.setTotal(total);
        categoryListVO.setLimit(limit);
        categoryListVO.setPages(pages);
        categoryListVO.setPage(page);
        categoryListVO.setList(list);

        return categoryListVO;
    }

    //创建类目的方法
    @Override
    public CategoryCreateVO categoryCreate(CategoryCreateBO categoryCreateBO) {
        MarketCategory marketCategory = new MarketCategory();
        try {
            BeanUtils.copyProperties(marketCategory,categoryCreateBO);
        } catch (Exception e) {
            e.printStackTrace();
        }

        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());
        marketCategory.setDeleted(false);

        marketCategoryMapper.insertSelective(marketCategory);

        CategoryCreateVO categoryCreateVO = new CategoryCreateVO();
        try {
            BeanUtils.copyProperties(categoryCreateVO,marketCategory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return categoryCreateVO;
    }

    //更新类目的方法
    @Override
    public void categoryUpdate(CategoryUpdateBO categoryUpdateBO) {
        MarketCategory marketCategory = new MarketCategory();
        try {
            BeanUtils.copyProperties(marketCategory,categoryUpdateBO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        marketCategory.setUpdateTime(new Date());
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    //删除类目的方法->实际上是将状态位修改为0
    @Override
    public void categoryDelete(InnerCategoriesOfCategoryListVO categoryDeleteBO) {
        List<MarketCategory> marketCategories = new ArrayList<>();

        for (ChildrenOfInnerCategoriesOfCategoryListVO child : categoryDeleteBO.getChildren()) {
            MarketCategory marketCategory = new MarketCategory(child.getId(),new Date(),true);
            marketCategories.add(marketCategory);
        }
        MarketCategory e = new MarketCategory(categoryDeleteBO.getId(),new Date(),true);
        marketCategories.add(e);

        for (MarketCategory marketCategory : marketCategories) {
            marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
        }

        return;
    }

    /**
     * lixinhang 新增 进入小程序首页，展示全部类目的方法
     * @return
     */
    @Override
    public List<WXHomeIndexChannelVO> indexChannel() {
        MarketCategoryExample example = new MarketCategoryExample();
        example.setDistinct(true);                //去重
        example.setOrderByClause("add_time asc");//按照添加时间降序排序
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);  //未被删除
        criteria.andPidEqualTo(0);          //仅查询一级类目

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        List<WXHomeIndexChannelVO> channel = new ArrayList<>();

        for (MarketCategory marketCategory : marketCategories) {
            //TODO 后期如何数据库里的图片存放格式修改，这行代码也要改
            WXHomeIndexChannelVO wxHomeIndexChannelVO = new WXHomeIndexChannelVO(marketCategory.getId(), marketCategory.getName(), marketCategory.getIconUrl());
            channel.add(wxHomeIndexChannelVO);
        }

        return channel;
    }

    @Override
    public List<WXHomeIndexFloorGoodsVO> indexFloorGoodsList() {
        //查询按id升序的前5个pid为0,且deleted为false的类目，得到其idList
        //开启分页
        PageHelper.startPage(1,5);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.setDistinct(true);
        marketCategoryExample.setOrderByClause("id asc");
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andPidEqualTo(0);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);

        List<WXHomeIndexFloorGoodsVO> floorGoodsList = new ArrayList<>();

        for (MarketCategory marketCategory : marketCategories) {
            //调用下面的方法,查询该一级类目下的5件商品
            List<WXHomeIndexGoodsVO> goodsList = fillFloorGoodsList(marketCategory.getId());
            WXHomeIndexFloorGoodsVO wxHomeIndexFloorGoodsVO = new WXHomeIndexFloorGoodsVO(marketCategory.getName(),goodsList,marketCategory.getId());
            floorGoodsList.add(wxHomeIndexFloorGoodsVO);
        }

        return floorGoodsList;
    }

    //给出一个一级marketCategory的id,查询出其二级目录的列表，然后查询表下的前5件商品
    private List<WXHomeIndexGoodsVO> fillFloorGoodsList(Integer id) {
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andPidEqualTo(id);
        List<MarketCategory> secondCategories2 = marketCategoryMapper.selectByExample(example);
        List<Integer> ids = new ArrayList<>();
        for (MarketCategory secondCategory : secondCategories2) {
            ids.add(secondCategory.getId());
        }
        //开启分页
        PageHelper.startPage(1,5);
        MarketGoodsExample example2 = new MarketGoodsExample();
        example2.setDistinct(true);
        example2.setOrderByClause("add_time desc"); //按照上架时间排序
        MarketGoodsExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andDeletedEqualTo(false);
        criteria2.andCategoryIdIn(ids);
        List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExample(example2);
        List<WXHomeIndexGoodsVO> wxHomeIndexGoodsVOS = new ArrayList<>();
        for (MarketGoods marketGoods : marketGoodsList) {
            WXHomeIndexGoodsVO wxHomeIndexGoodsVO = new WXHomeIndexGoodsVO();
            try {
                BeanUtils.copyProperties(wxHomeIndexGoodsVO,marketGoods);
            } catch (Exception e) {
                e.printStackTrace();
            }
            wxHomeIndexGoodsVOS.add(wxHomeIndexGoodsVO);
        }

        return wxHomeIndexGoodsVOS;
    }
}
